package com.falcon.authority.api;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.authority.domain.dos.ApiDTO;
import com.falcon.authority.domain.po.ApiPO;
import com.falcon.authority.mapper.IApiMapper;
import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.OpType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author zongf
 * @date 2025-02-10
 **/
@Slf4j
@Service
public class ApiApi implements IApiApi{

    @Autowired
    private IApiMapper apiMapper;

    @Override
    public int saveOrUpdate(ApiDTO apiDTO) {
        ApiPO apiPO = BeanUtil.copyProperties(apiDTO, ApiPO.class);
        int success = this.apiMapper.insertOnDupKey(apiPO);
        apiDTO.setId(apiPO.getId());
        return success;
    }

    @Override
    public ApiDTO selectByRequestUri(String requestUri) {
        Condition condition = Condition.newInstance()
                .and(ApiPO.Fields.requestUri, OpType.equals, requestUri);
        ApiPO apiPO = this.apiMapper.select(condition);

        return Objects.nonNull(apiPO) ? BeanUtil.copyProperties(apiPO, ApiDTO.class) : null;
    }

    @Override
    public int updateStatus(int status) {
        return this.apiMapper.updateStatus(status);
    }

    @Override
    public int count(Condition condition) {
        return this.apiMapper.count(condition);
    }
}
